<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="device-app">
    <div class="container">
        <div class="tableBar">
            <div class="tableLab">
                设备名称: <el-input v-model="conditions.name" placeolder="输入设备名称" style="width: 250px" clearable></el-input>
                设备标识: <el-input v-model="conditions.deviceKey" placeolder="输入设备标识" style="width: 250px" clearable></el-input>
                <el-select v-model="conditions.deviceTypeId" placeholder="选择设备类型">
                    <el-option
                            v-for="item in deviceTypeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
                <el-select v-model="conditions.active" placeholder="选择设备激活状态">
                    <el-option
                            v-for="item in activeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
                <el-button  type="primary" icon="el-icon-search" @click="handleQuery()">搜索</el-button>
            </div>
        </div>
        <el-table  :data="pagination.tableData"  stripe  class="tableBox" >
            <el-table-column type="index" width="50" label="序号"></el-table-column>
            <el-table-column  prop="name" label="设备名称"  /></el-table-column>
            <el-table-column  prop="deviceKey" label="设备标识" width="200" /></el-table-column>
            <el-table-column  label="设备类型"/>
                <template slot-scope="scope">
                    <span v-for="item in deviceTypeList" v-if="scope.row.deviceTypeId==item.id">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column  label="所属产品"/>
                <template slot-scope="scope" >
                    <span v-for="item in productList" v-if="scope.row.productId==item.id">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column label="父设备">
                <template slot-scope="scope" v-for="item in parentDeviceList">
                    <span v-if="scope.row.parentId==item.id">{{item.name}}</span>
                    <span v-else>无</span>
                </template>
            </el-table-column>
            <el-table-column  label="所属租户"/>
                <template slot-scope="scope">
                    <span v-for="item in tenantList" v-if="scope.row.tenantId==item.id">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column  label="激活状态"/>
                <template slot-scope="scope">
                    <span v-if="scope.row.active==1"><el-tag type="info">未激活</el-tag></span>
                    <span v-if="scope.row.active==2"><el-tag type="success">激活</el-tag></span>
                </template>
            </el-table-column>
            <el-table-column  label="在线状态"/>
            <template slot-scope="scope">
                <span v-if="scope.row.state==false"><el-tag type="info">离线</el-tag></span>
                <span v-if="scope.row.state==true"><el-tag type="success">在线</el-tag></span>
            </template>
            </el-table-column>
            <el-table-column prop="createTime"  label="创建日期" width="180" >
                <template slot-scope="scope">
                    {{scope.row.createTime}}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="toDeviceValue(scope.row.deviceKey)"
                               v-for="item in deviceTypeList" v-if="(item.id==scope.row.deviceTypeId && item.deviceType==2)
                               || (item.id==scope.row.deviceTypeId && item.deviceType==1)">查询数据</el-button>

                    <el-button type="text" size="small" class="delBut non" @click="toCmd(scope.row.deviceKey)"
                               v-for="item in deviceTypeList" v-if="item.id==scope.row.deviceTypeId && item.deviceType!=2">发送指令</el-button>

                    <el-button type="text" size="small" class="delBut non" @click="toCmdValue(scope.row.deviceKey)"
                               v-for="item in deviceTypeList" v-if="item.id==scope.row.deviceTypeId && item.deviceType!=2">指令日志</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.counts"
                @size-change="handleSizeChange"
                :current-page.sync="pagination.page"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="设备名称：">
                <el-input v-model="classData.tempForm.name" maxlength="50" :disabled="true"/>
            </el-form-item>
            <el-form-item label="设备Key：">
                <el-input v-model="classData.dialogForm.deviceKey"  maxlength="50" :disabled="true"/>
            </el-form-item>
            <el-form-item label="继电器编号：">
                <el-input v-model="classData.dialogForm.deviceSubCode" placeholder="请输入继电器编号" maxlength="50"/>
            </el-form-item>
            <el-form-item label="指令名称：">
                <el-select v-model="classData.dialogForm.cmdName" placeholder="选择指令名称">
                    <el-option
                            v-for="(value, key) in modelList"
                            :key="key"
                            :label="value"
                            :value="key">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="指令值：">
                <el-input v-model="classData.dialogForm.value" placeholder="请输入指令值" maxlength="50"/>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleClose()">取 消</el-button>
        <el-button type="primary" size="medium" @click="submitForm()">发 送</el-button>
      </span>
    </el-dialog>
</div>
<script src="../../plugins/vue/vue.js"></script>
<script src="../../plugins/element-ui/index.js"></script>
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/device.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#device-app',
        data() {
            return {
                action: '',
                type: '',
                pagination: {
                    tableData: [],
                    page: 1,
                    pageSize: 10,
                    counts:0,
                },
                conditions:{
                    name:'',
                    deviceKey:'',
                    deviceTypeId:'',
                    active:''
                },
                classData: {
                    'title': '添加设备',
                    'dialogVisible': false,
                     dialogForm:{},
                    tempForm:{}
                },
                deviceTypeList:[],
                tenantList:[],
                parentDeviceList:[],
                productList:[],
                modelList:[],
                activeList:[
                    {
                        'id':'1',
                        'name':'未激活'
                    },
                    {
                        'id':'2',
                        'name':'激活'
                    }
                ]
            }
        },
        methods: {
            async init() {
                const params = {
                    page: {
                        current: this.pagination.page,//当前页码
                        size: this.pagination.pageSize,//每页大小
                    },
                    name: this.conditions.name ? this.conditions.name : undefined,
                    deviceKey: this.conditions.deviceKey ? this.conditions.deviceKey : undefined,
                    deviceTypeId: this.conditions.deviceTypeId ? this.conditions.deviceTypeId : undefined,
                    active: this.conditions.active ? this.conditions.active : undefined
                };
                await findDevicePage(params).then(res => {
                    if (String(res.code) === '200') {
                        this.pagination.tableData = res.data.records;
                        this.pagination.counts = res.data.total;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            //跳转到数据查看页面
            toDeviceValue(deviceKey){
                window.parent.menuHandle({
                    id: '64',
                    url: 'page/deviceData/list.html?device=' + deviceKey,
                    name: '设备数据'
                }, true);
            },
            //跳转到数据查看页面
            toCmdValue(deviceKey){
                window.parent.menuHandle({
                    id: '64',
                    url: 'page/deviceData/cmdList.html?deviceKey=' + deviceKey,
                    name: '设备指令日志'
                }, true);
            },
            //弹出发送指令窗口
            toCmd(deviceKey){
                //赋值
                this.classData.title = '向设备发送指令';
                this.action = 'send';
                findDeviceByDeviceKey(deviceKey).then(res => {
                    if (String(res.code) === '200') {
                        this.classData.tempForm  = res.data;
                        this.classData.dialogForm.deviceKey = deviceKey;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
                this.classData.dialogVisible = true;//打开修改框
            },
            //数据提交
            submitForm(st) {
                const formData = this.classData.dialogForm;
                if (this.action === 'send') {
                    sendCmd(formData).then(res => {
                        if (res.code === 200) {
                            this.$message.success('发送成功！')
                            this.classData.dialogVisible = false
                            this.classData.dialogForm = {};
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                }
            },
            //分页相关全部操作
            handleSelectionChange(val) {
                let checkArr = []
                val.forEach((n) => {
                    checkArr.push(n.id)
                })
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            //查询所有设备类型
            async initDeviceTypeList(){
                await findDeviceTypeList().then(res => {
                    if (String(res.code) === '200') {
                        this.deviceTypeList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有产品
            async initProductList(){
                await findProductList().then(res => {
                    if (String(res.code) === '200') {
                        this.productList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有设备
            async initDeviceList(){
                const deviceParams = {
                    parentId:'0'
                };
                await findDeviceList(deviceParams).then(res => {
                    if (String(res.code) === '200') {
                        this.parentDeviceList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有租户
            async initTenantList(){
                await findTenantList().then(res => {
                    if (String(res.code) === '200') {
                        this.tenantList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有物模型
            async initModelList(){
                await findModelList().then(res => {
                    if (String(res.code) === '200') {
                        this.modelList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            }
        },
        created() {
            this.init();
            this.initTenantList();
            this.initDeviceList();
            this.initProductList();
            this.initDeviceTypeList();
            this.initModelList();
        }
    })
</script>
</body>
</html>